package com.ethan.cj.ssm.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @Description 日期常用工具类
 * @author jet.dong
 * @date 2015年7月21日 下午12:05:36
 * @version V1.0.0
 */
public class DateUtil {

	public static final String DATE_DFT_PATTERN = "yyyy-MM-dd";
	public static final String DATETIME_DFT_PATTERN = "yyyy-MM-dd HH:mm:ss";

	public static String getCurrentDate() {
		return getCurrentDate(new Date(), DATE_DFT_PATTERN);
	}

	public static String getCurrentDate(String pattern) {
		return getCurrentDate(new Date(), pattern);
	}

	public static String getCurrentDate(Date date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}

	public static String getCurrentDateTime() {
		return format(new Date(), DATETIME_DFT_PATTERN);
	}

	public static String getCurrentDateTime(String pattern) {
		return format(new Date(), pattern);
	}

	public static String format(long datetime) {
		Date date = new Date(datetime);
		return format(date);
	}

	public static String format(long datetime, String pattern) {
		Date date = new Date(datetime);
		return format(date, pattern);
	}

	public static String format(Date date) {
		return format(date, DATE_DFT_PATTERN);
	}

	public static String formatdate(Date date) {
		return format(date, DATETIME_DFT_PATTERN);
	}

	public static String format(Date date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}

	public static String format(String dateStr) {
		Date date = parse(dateStr, DATETIME_DFT_PATTERN);
		return format(date, DATETIME_DFT_PATTERN);
	}

	public static String formatdate(String dateStr) {
		Date date = parse(dateStr, DATE_DFT_PATTERN);
		return format(date, DATE_DFT_PATTERN);
	}

	public static String format(String dateStr, String pattern) {
		Date date = parse(dateStr, DATETIME_DFT_PATTERN);
		return format(date, pattern);
	}

	public static String format(String dateStr, String orgPattern, String pattern) {
		Date date = parse(dateStr, orgPattern);
		return format(date, pattern);
	}

	public static Date parse(String dateStr) {
		return parse(dateStr, DATE_DFT_PATTERN);
	}

	public static Date parsetime(String dateStr) {
		return parse(dateStr, DATETIME_DFT_PATTERN);
	}

	public static Date parse(String dateStr, String pattern) {
		Date date = null;
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static Date parse(Date date) {
		return parse(date, DATE_DFT_PATTERN);
	}

	public static Date parse(Date date, String pattern) {
		String dateStr = format(date, DATETIME_DFT_PATTERN);
		return parse(dateStr, pattern);
	}

	public static Date parse(long datetime) {
		return new Date(datetime);
	}

	public static Date parse(long datetime, String pattern) {
		Date date = new Date(datetime);
		return parse(date, pattern);
	}

	public static String parseStr(String dateStr, String pattern) {
		Date date = parse(dateStr);
		return format(date, pattern);
	}

	public static String parseTry(String dateStr, String pattern) {
		try {
			dateStr = dateStr.replace("年", "-").replace("月", "-").replace("日", "").replace("/", "-").replace(".", "-");
			if (dateStr.length() == 10) {
				return dateStr;
			}
			if (dateStr.length() == 9) {
				if (dateStr.substring(6, 7).equals("-"))// 第七个字符如果是-
					dateStr = dateStr.substring(0, 5) + "0" + dateStr.substring(5);
				else
					dateStr = dateStr.substring(0, 7) + "-" + dateStr.substring(7);
				return dateStr;
			}
			if (dateStr.length() == 8) {
				if (dateStr.substring(4, 5).equals("-"))// 第⑤个字符如果是-
					dateStr = dateStr.substring(0, 5) + "0" + dateStr.substring(5, 7) + "0" + dateStr.substring(7);
				else {
					dateStr = dateStr.substring(0, 4) + "-" + dateStr.substring(4, 6) + "-" + dateStr.substring(6);
				}
				return dateStr;
			}
			return "";
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}

	}

	public static Date getFirstOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		return cal.getTime();
	}

	public static Date getFirstOfMonth(long datetime) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(datetime);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		return cal.getTime();
	}

	public static Date getLastOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.add(Calendar.MONTH, 1);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		return cal.getTime();
	}

	public static Date getLastOfMonth(long datetime) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(datetime);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.add(Calendar.MONTH, 1);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		return cal.getTime();
	}

	public static Date addDate(Date date, int field, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(field, amount);
		return cal.getTime();
	}

	public static String addDateOfDay(Date date, int amount) {
		return format(addDate(new Date(), Calendar.DAY_OF_MONTH, amount));
	}

	public static String beforeDateOfDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -1); // 得到前一天
		Date date = calendar.getTime();
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		return df.format(date).toString();
	}

	public static String afterDateOfDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, 1); // 得到前一天
		Date date = calendar.getTime();
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		return df.format(date).toString();
	}

	public static String beforeDateOfYear() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.YEAR, -1); // 得到前一天
		Date date = calendar.getTime();
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		return df.format(date).toString();
	}

	public static String beforeDateOfYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.YEAR, -1); // 得到前一天
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		return df.format(date).toString();
	}

	public static String afterDateOfYear() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.YEAR, 1); // 得到前一天
		Date date = calendar.getTime();
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		return df.format(date).toString();
	}

	public static String afterDateOfYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.YEAR, 1); // 得到前一天
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		return df.format(date).toString();
	}

	public static String currentDate() {
		return DateUtil.getCurrentDate("yyyyMMdd");
	}

	public static void main(String[] args) {
		int m = 0;
		try {
			m = getMonthsOfAge("201612", "201702");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(m);
	}

	public static Timestamp getSysTimeStamp() {

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// 定义格式，不显示毫秒
		Timestamp now = new Timestamp(System.currentTimeMillis());
		// 获取系统当前时间
		String str = df.format(now);
		// String转化为Timestamp:
		SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = dff.format(new Date());
		Timestamp ts = Timestamp.valueOf(time);
		return ts;
	}

	public static Timestamp getSystemTime() {
		Date dt = new Date();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowTime = df.format(dt);
		Timestamp buydate = Timestamp.valueOf(nowTime);

		return buydate;
	}

	/**
	 * 计算两个日期之间相差的天数
	 * 
	 * @param smdate 较小的时间
	 * @param bdate 较大的时间
	 * @return 相差天数
	 * @throws ParseException
	 */
	public static int daysBetween(Date smdate, Date bdate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		smdate = sdf.parse(sdf.format(smdate));
		bdate = sdf.parse(sdf.format(bdate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 字符串的日期格式的计算
	 */
	public static int daysBetween(String smdate, String bdate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(smdate));
		long time1 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(bdate));
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 获取传入时间（YYYYMM）前后月份值 sean.xie 20160901
	 */
	public static int getFrontOrBackTime(String date, int m) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");// 格式化为年月
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(sdf.parse(date));
		calendar.add(Calendar.MONTH, m);
		return Integer.parseInt(sdf.format(calendar.getTime()));
	}

	/**
	 * 获取两个时间（YYYYMM）的月份之差 sean.xie 20170103
	 */
	public static int getMonthsOfAge(String start, String end) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");// 格式化为年月
		Calendar cStart = Calendar.getInstance();
		cStart.setTime(sdf.parse(start));
		Calendar cEnd = Calendar.getInstance();
		cEnd.setTime(sdf.parse(end));
		return (cEnd.get(Calendar.YEAR) - cStart.get(Calendar.YEAR)) * 12 + cEnd.get(Calendar.MONTH) - cStart.get(Calendar.MONTH);
	}

	/**
	 * 开始时间与结束时间之差
	 */
	public static int getDateDifference(String contract_Start, String contract_End) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
		Date contract_StartDate = sdf.parse(contract_Start);
		Date contract_EndDate = sdf.parse(contract_End);
		int daySum = DateUtil.daysBetween(contract_StartDate, contract_EndDate);
		return daySum;
	}



	/**
	 * 
	 * @Description 获得当前日期的日
	 * @author Ada.liu
	 * @return
	 */
	public static int getDayOfMonth() throws ParseException {
		Calendar rightNow = Calendar.getInstance();
		return rightNow.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 
	 * @Description 获得系统时间（精确到毫秒）
	 * @author Ada.liu
	 * @return
	 */
	public static String getSystemMsTime() {
		Date dt = new Date();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		String nowTime = df.format(dt);
		return nowTime;
	}

	/**
	 * 获取当前时间往前推几个月
	 */
	public static String getMouth(int num) {
		Calendar calendar = Calendar.getInstance();// 此时打印它获取的是系统当前时间
		calendar.add(Calendar.MONTH, num); // 得到前n个月
		String resultDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(calendar.getTime());
		return resultDate;
	}


}
